package com.xxx.blog.controller;


import com.github.pagehelper.PageInfo;
import com.xxx.base.utils.R;
import com.xxx.blog.domain.Article;
import com.xxx.blog.query.ArticleQuery;
import com.xxx.blog.service.IArticleService;
import com.xxx.blog.vo.GdVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController//@rc
@RequestMapping("/article")
@Api(tags = "文章接口类")
public class ArticleController {
    @Autowired
    private IArticleService articleService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public R<List<Article>> findAll() {
        List<Article> articles = articleService.findAll();
        System.out.println(articles);
        return R.ok(articles);//alt enter生成返回值
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public R<Article> findOne(@PathVariable Long id) {
        Article article = articleService.findOne(id);
        return R.ok(article);//alt enter生成返回值
    }

    @PostMapping
    @ApiOperation(value = "分页+条件查询")
    public PageInfo<Article> queryPage(@RequestBody ArticleQuery articleQuery) {
        List<Article> list = articleService.queryPage(articleQuery);
        return new PageInfo<Article>(list);//alt enter生成返回值,list分页集合
    }

    @PutMapping
    @ApiOperation(value = "增加or修改")
    public R<Void> addOrUpdate(@RequestBody Article article) {
        //try
        try {
            Long id = article.getId();

            if (id==null){
                articleService.addArticle(article);
            }else {
                articleService.updateArticle(article);
            }
        } catch(Exception e) {
            e.printStackTrace();
            return R.fail();
        }

        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个")
    public R<Void> deleteArticle(@PathVariable Long id) {
        //try
        try {
            articleService.deleteArticle(id);

        } catch(Exception e) {
            e.printStackTrace();
            return R.fail();
        }

        return R.ok();
    }

    @PatchMapping
    @ApiOperation(value = "批量删除")
    public R<Void> batchArticles(@RequestBody Long[] ids) {
        try {
            articleService.batchArticles(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }

        return R.ok();
    }
    @GetMapping("/PlaceOnFile/")
    @ApiOperation(value = "查询归档")
    public List<GdVo> getPlaceOnFileVo() {
         List<GdVo> list  = articleService.getPlaceOnFileVo();
        return list;
    }
    @GetMapping("/ra/{id}")
    @ApiOperation(value = "查询相关文章的集合")
    public List<Article> getRa(@PathVariable Long id) {
        List<Article> list  = articleService.getRa(id);
        return list;
    }
    @GetMapping("/getByFileName/{fileName}")
    @ApiOperation(value = "查询单个")
    public R<Article> getByFileName(@PathVariable String fileName) {
        Article article = articleService.getByFileName(fileName);
        return R.ok(article);//alt enter生成返回值
    }
}
